package de.vocabulary.client.trainer;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.vocabulary.client.LanguageModuleManager;
import de.vocabulary.client.VocabularyServiceProvider;
import de.vocabulary.client.editor.ComboBoxWidget;
import de.vocabulary.client.editor.AddVocabulary.GetLessonsCallback;
import de.vocabulary.client.service.VocabularyService;
import de.vocabulary.client.service.VocabularyServiceAsync;
import de.vocabulary.shared.model.LanguageModule;
import de.vocabulary.shared.model.Vocable;

public class VocabularyTrainer extends Composite {

	private static VocabularyTrainerUiBinder uiBinder = GWT
			.create(VocabularyTrainerUiBinder.class);

	interface VocabularyTrainerUiBinder extends
	UiBinder<Widget, VocabularyTrainer> {
	}

	VocabularyServiceAsync service;
	LanguageModule languageModule;
	
	protected void onLoad(){

		languageModule = LanguageModuleManager.getLanguageModuleManager().getLanguageModule();

		service = VocabularyServiceProvider.getService();
		
		lessons.setVisibleItemCount(1);
		query.setVisible(false);
		service.getLessons(new GetLessonsCallback());
		amountVocabularyTextBox.setValue(amountVocabulary);
		ALabel.setText(languageModule.getSourceLanguage());
		BLabel.setText(languageModule.getTargetLanguage());
		
	}

	public VocabularyTrainer() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	//--- Initiation------------------------------

	int amountVocabulary = 2;
	
	@UiField
	ListBox lessons;
	
	@UiField
	Label ALabel;
	
	@UiField
	Label BLabel;


	@UiField
	VerticalPanel content;
	
	@UiField
	RadioButton ABRadioButton;
	
	@UiField
	RadioButton ABARadioButton;
	
	@UiField
	RadioButton BARadioButton;
	
	@UiField
	IntegerBox amountVocabularyTextBox;
	
	@UiField
	CheckBox withPronunciationCheckbox;
	
	
	@UiField
	Button startButton;
	
	/*@UiHandler("amountVocabularyTextBox")
	public void checkInputValidity(ChangeEvent e){
		try{
			amountVocabulary = Integer.parseInt(amountVocabularyTextBox.getText());
		}catch(NumberFormatException ex){
			Window.alert("Enter a number");
			amountVocabularyTextBox.setText("");
		}
		
	}*/
	
	

	@UiHandler("startButton")
	public void startTraining(ClickEvent event){
		content.setVisible(false);
		query.setVisible(true);
		withPronunciation = withPronunciationCheckbox.getValue();
		direction = (ABARadioButton.getValue())?"ABA":(ABRadioButton.getValue())?"AB":"BA";
		amountVocabulary = amountVocabularyTextBox.getValue(); 
		service.setParametersAndGetFirstVocable(lessons.getItemText(lessons.getSelectedIndex()), amountVocabulary , 
				new GetVocableCallback());
	}
	
	//--- Actual Training -------------------------
	
	String direction;
	boolean withPronunciation;
	
	
	@UiField
	VerticalPanel query;
	
	@UiField
	HorizontalPanel questionButtons;
	
	@UiField
	HorizontalPanel answerButtons;
	
	@UiField
	Label lang1Word;
	
	@UiField
	Label lang2Word;
	
	private Vocable currentVocable;
	
		
	private void showNextQuestion(){
		if (currentVocable==null){
			Window.alert("Done");
		}else{
			lang1Word.setText(currentVocable.getLanguage1Word());	
			lang2Word.setText("");
			questionButtons.setVisible(true);
			answerButtons.setVisible(false);
		}
	}
	
	@UiHandler("correctButton")
	void correct(ClickEvent event){
		lang2Word.setText(currentVocable.getLanguage2Word());
		questionButtons.setVisible(false);
		answerButtons.setVisible(true);
	}
	
	@UiHandler("wrongButton")
	void wrongAnswer(ClickEvent event){
		service.submitFeedbackAndGetNextVocable(false, false, new GetVocableCallback());
	}
	
	@UiHandler("rightButton")
	void rightAnswer(ClickEvent event){
		service.submitFeedbackAndGetNextVocable(true, false, new GetVocableCallback());
	}
	
	@UiHandler("ignoreButton")
	void ignoreAnswer(ClickEvent event){
		service.submitFeedbackAndGetNextVocable(false, true, new GetVocableCallback());
	}
	
	
	
	//----- Callback Methods ------------------------

	public class GetLessonsCallback implements AsyncCallback<List<String>> {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());

		}


		@Override
		public void onSuccess(List<String> result) {
			for (String lesson: result){
				lessons.addItem(lesson);
			}

		}

	}
	
	public class GetVocableCallback implements AsyncCallback<Vocable> {

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());

		}


		@Override
		public void onSuccess(Vocable result) {
			currentVocable = result;
			showNextQuestion();

		}

	}


}
